package middlewares

import (
	"github.com/gin-gonic/gin"
	"mts/trans_srv/internal/domain"
	"net/http"
)

func IsAdminAuth() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		claims, exists := ctx.Get("claims")
		if !exists {
			ctx.JSON(http.StatusForbidden, gin.H{"msg": "未找到认证信息"})
			ctx.Abort()
			return
		}

		customClaims, ok := claims.(*domain.CustomClaims)
		if !ok {
			ctx.JSON(http.StatusForbidden, gin.H{"msg": "无效的认证信息"})
			ctx.Abort()
			return
		}

		if customClaims.AuthorityId != 2 {
			ctx.JSON(http.StatusForbidden, gin.H{"msg": "无权限"})
			ctx.Abort()
			return
		}

		ctx.Next()
	}
}
